<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */
 
class Admin_BuyDeptController extends Zenddn_Controller_Action{
	
	public function init(){
		parent::init();
        parent::loadTemplate('alpona');
        $this->view->headTitle = "manage dept";
	}
	
	public function indexAction(){
		$scripts = array('jquery.dataTables.js', 'dataTables.bootstrap.js', 'TableTools.js');
		parent::addScripts($scripts);
		$model = new Admin_Model_BuyDept();
        $data = $model->getDepts();
        
        $this->view->action = 'create';
        $this->view->data = $data;
	}
    
    public function addAction(){
    	$request = $this->_request;
        
        if($request->isPost()){
            $validator = new Admin_Form_ValidateBuyDept($request->getParams());
            
            if(!$validator->isError()){
                $model = new Admin_Model_BuyDept();
                $model->saveData($validator->getData());
                
                $this->_helper->_redirector->gotoRoute(array('module' => 'admin', 'controller' => 'buy-dept', 'action' => 'index'), null, true);
            }
        }
        
        $this->_helper->viewRenderer->setNoRender();
    }
    
    public function viewAction(){
    	$scripts = array('jquery.dataTables.js', 'dataTables.bootstrap.js', 'TableTools.js');
		parent::addScripts($scripts);
		$request = $this->_request;
        
        if($request->isPost()){
            $supplier_id = $request->getParam('supplier_id');
            
            $model = new Admin_Model_BuyDept();
            $data = $model->getData($supplier_id);
            
            $this->view->data = $data;
        }        
    }
    
    public function removeAction(){
    	$request = $this->_request;
        
        if($request->isPost()){
            $model = new Admin_Model_BuyDept();
            $model->deleteData($request->getParam('id'));
            
            $this->_helper->_redirector->gotoRoute(array('module' => 'admin', 'controller' => 'buy-dept', 'action' => 'index'), null, true);
        }
        
        $this->_helper->viewRenderer->setNoRender();
    }
}